package itsm.isperp.module.service.wechat;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.service.TreeNodeService;
import itsm.isperp.framework.template.Template;
import itsm.isperp.framework.template.TemplateException;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.wechat.WechatMenu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import me.chanjar.weixin.common.bean.WxMenu;
import me.chanjar.weixin.common.bean.WxMenu.WxMenuButton;

import org.springframework.util.Assert;

/**
 * 
 * 
 * 
 * @author lizx
 * @date 2015-7-28
 * @since 1.0
 * 
 */
public abstract class WechatMenuService<T extends WechatMenu> extends
		TreeNodeService<T> {

	public abstract WechatMenu findOneByKey(String menuKey);

	@Override
	public <S extends T> int saveOrUpdate(S entity, HttpServletRequest req) {
		Assert.notNull(entity, "对象不能为空");

		if ("view".equals(entity.getMenuType())) {
			entity.setMessageContent("");
			entity.setReplayType("");
		} else {

			entity.setUrl("");
			String replayType = entity.getReplayType();
			switch (replayType) {
			case "":
				entity.setMessageContent("");
				break;
			case "text":
			case "customer":
				String messageContentArea = req
						.getParameter("messageContentArea");
				entity.setMessageContent(messageContentArea);

				break;
			case "news":
				String newsTitle = req.getParameter("newsTitle");
				entity.setMessageContent(newsTitle);
				break;

			}
		}
		return this.saveOrUpdate(entity);
	}

	public WxMenu createWechatMenu() throws TemplateException {

		List<T> list = this.findAll(new JqGridRequest().isPageable(false)
				.addFilterRule("parent_id", JqGridFilterRuleOp.isnull, ""));
		int listSize = list.size();
		List<WxMenuButton> buttons = new ArrayList<>();
		Template t = new Template();
		Map<String, Object> vars = new HashMap<>();

		String url = ContextHolder.getWebServerName();
		vars.put("config", ContextHolder.CONFIG_RESOURCES_MAP);
		vars.put("contextUrl", url);

		for (int i = 0; i < listSize; i++) {
			T menu = list.get(i);
			WxMenuButton b = new WxMenuButton();

			b.setKey(menu.getMenuKey());
			b.setName(menu.getName());
			b.setType(menu.getMenuType());

			if ("view".equals(menu.getMenuType())) {
				t.setSource(menu.getUrl());
				t.setContext(vars);

				b.setUrl(t.render());
			}

			List<T> sub = this.findAll(new JqGridRequest().isPageable(false)
					.addFilterRule("parent_id", JqGridFilterRuleOp.eq,
							menu.getId()));
			if (sub != null && sub.size() > 0) {
				List<WxMenuButton> subButtons = new ArrayList<WxMenuButton>();
				b.setType(null);
				b.setKey(null);
				for (int j = 0; j < sub.size(); j++) {
					T menu_sub = sub.get(j);
					WxMenuButton b_sub = new WxMenuButton();

					b_sub.setKey(menu_sub.getMenuKey());
					b_sub.setName(menu_sub.getName());
					b_sub.setType(menu_sub.getMenuType());

					if ("view".equals(menu_sub.getMenuType())) {
						t.setSource(menu_sub.getUrl());
						t.setContext(vars);

						b_sub.setUrl(t.render());
					}

					subButtons.add(b_sub);
				}
				b.setSubButtons(subButtons);
			}

			buttons.add(b);
		}

		WxMenu mb = new WxMenu();
		mb.setButtons(buttons);

		return mb;
	}

}
